package icu.qx.pro.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import icu.qx.common.pojo.vo.IndexCondition;
import icu.qx.pro.domain.ProWorkOrder;
import icu.qx.pro.domain.gbs.GbsBomMaterial;
import icu.qx.pro.domain.pbs.PbsProcedure;
import icu.qx.pro.domain.pbs.PbsProcedureRoute;
import icu.qx.pro.domain.pbs.PbsWorkCell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.pro.domain.ProSchedule;
import icu.qx.pro.service.IProScheduleService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产计划Controller
 *
 * @author yong
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/pro/schedule")
public class ProScheduleController extends BaseController {
    private IProScheduleService proScheduleService;

    @Autowired
    public void setProScheduleService(IProScheduleService proScheduleService) {
        this.proScheduleService = proScheduleService;
    }

    /**
     * 查询生产计划列表
     */
    @RequiresPermissions("pro:schedule:list")
    @GetMapping("/list")
    public TableDataInfo list(ProSchedule proSchedule) {
        startPage();
        List<ProSchedule> list = proScheduleService.selectProScheduleList(proSchedule);
        return getDataTable(list);
    }

    /**
     * 查询工单任务条件的内容
     */
    @RequiresPermissions("pro:workTask:list")
    @GetMapping("/condition")
    public AjaxResult condition() {
        AjaxResult ajaxResult = success();
        List<IndexCondition> indexCondition = proScheduleService.selectScheduleCondition();
        ajaxResult.put("data", indexCondition);
        return ajaxResult;
    }

    /**
     * 导出生产计划列表
     */
    @RequiresPermissions("pro:schedule:export")
    @Log(title = "生产计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProSchedule proSchedule) {
        List<ProSchedule> list = proScheduleService.selectProScheduleList(proSchedule);
        ExcelUtil<ProSchedule> util = new ExcelUtil<ProSchedule>(ProSchedule.class);
        util.exportExcel(response, list, "生产计划数据");
    }

    /**
     * 获取生产计划详细信息
     */
    @RequiresPermissions("pro:schedule:query")
    @GetMapping(value = "/{scheduleCode}")
    public AjaxResult getInfo(@PathVariable("scheduleCode") String scheduleCode) {
        AjaxResult ajaxResult = success();
        ProSchedule data = proScheduleService.selectProScheduleByScheduleCode(scheduleCode);
        ajaxResult.put("data", data);
        ajaxResult.put("gbsGood", "");
        return ajaxResult;
    }

    /**
     * 新增生产计划
     */
    @RequiresPermissions("pro:schedule:add")
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProSchedule proSchedule) {
        return toAjax(proScheduleService.insertProSchedule(proSchedule));
    }

    /**
     * 修改生产计划
     */
    @RequiresPermissions("pro:schedule:edit")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProSchedule proSchedule) {
        return toAjax(proScheduleService.updateProSchedule(proSchedule));
    }

    /**
     * 分解生产计划，生成生产工单
     */
    @RequiresPermissions("pro:schedule:edit")
    @PutMapping("/decompose")
    public AjaxResult decompose(@RequestBody List<ProWorkOrder> proWorkOrders) {
        return success(proScheduleService.decomposeProSchedule(proWorkOrders));
    }

    /**
     * 获取相关的生产工单
     */
    @RequiresPermissions("pro:schedule:query")
    @GetMapping("/getOrders")
    public AjaxResult getOrders(@RequestParam("scheduleCode") String scheduleCode) {
        return success(proScheduleService.selectRelatedOrders(scheduleCode));
    }

    /**
     * 删除生产计划
     */
    @RequiresPermissions("pro:schedule:remove")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scheduleCodes}")
    public AjaxResult remove(@PathVariable String[] scheduleCodes) {
        return toAjax(proScheduleService.deleteProScheduleByScheduleCodes(scheduleCodes));
    }
}
